package com.wsd.jing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.jing.entity.Parameter;
import com.wsd.jing.enums.CodeEnum;
import com.wsd.jing.exception.BusinessException;
import com.wsd.jing.mapper.ParameterMapper;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.service.IParameterService;
import com.wsd.jing.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.service.impl
 * @className: ParameterServiceImpl
 * @description: 参数服务实现类
 * @createDate 2022-11-20 07:41
 * @copyright 2022-11-20 07:41 www.tom.com Inc. All rights reserved.
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ParameterServiceImpl extends ServiceImpl<ParameterMapper, Parameter> implements IParameterService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertParameter(Parameter parameter) {
        QueryWrapper<Parameter> parameterQueryWrapper = new QueryWrapper<>();
        parameterQueryWrapper.lambda().eq(Parameter::getKeyName, parameter.getKeyName());
        Parameter parameter1 = this.getOne(parameterQueryWrapper);
        if (ObjectUtil.isNotEmpty(parameter1)) {
            throw new BusinessException(CodeEnum.PARAMETER_KEY_EXIST);
        }
        parameter.setCreateTime(LocalDateTime.now());
        return this.save(parameter);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateParameter(Parameter parameter) {
        QueryWrapper<Parameter> parameterQueryWrapper = new QueryWrapper<>();
        parameterQueryWrapper.lambda().eq(Parameter::getKeyName, parameter.getKeyName());
        Parameter parameter1 = this.getOne(parameterQueryWrapper);
        if (ObjectUtil.isNotEmpty(parameter1) && !parameter.getId().equals(parameter1.getId())) {
            throw new BusinessException(CodeEnum.PARAMETER_KEY_EXIST);
        }
        parameter.setUpdateTime(LocalDateTime.now());
        return this.updateById(parameter);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteParameter(Integer id) {
        // TODO 删除时需判断在其他业务表中是否使用该记录
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteParameters(String ids) {
        List<String> idList = StrUtil.splitTrim(ids, ",");
        // TODO 删除时需判断在其他业务表中是否使用该记录
        return this.removeByIds(idList);
    }

    @Override
    public Parameter getParameter(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<Parameter> listParameters(CommonRequest commonRequest, Parameter parameter) {
        QueryWrapper<Parameter> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handleListSort(commonRequest, queryWrapper);
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> listPageParameters(CommonRequest commonRequest, Parameter parameter) {
        Map<String, Object> resultMap = new HashMap<>(2);
        Page<Parameter> page = new Page<>(commonRequest.getPageNum(), commonRequest.getPageSize());
        QueryWrapper<Parameter> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件
        if (StrUtil.isNotBlank(parameter.getName())) {
            queryWrapper.lambda().like(Parameter::getName, parameter.getName());
        }
        if (ObjectUtil.isNotNull(parameter.getStatus())) {
            queryWrapper.lambda().eq(Parameter::getStatus, parameter.getStatus());
        }
        // 处理排序字段
        SortUtil.handlePageSort(commonRequest, page);
        IPage<Parameter> result = this.page(page, queryWrapper);
        resultMap.put("rows", result.getRecords());
        resultMap.put("total", result.getTotal());
        return resultMap;
    }
}
